# 6.1 简单字典
alien_0 = {'color': 'green', 'points':5}
print(alien_0['color'])
print(alien_0['points'])

# 6.2 使用字典
alien_0 = {'color': 'green', 'points':5}
alien_0 = {'color': 'green'}

# 6.2.1 访问字典中的值
print(alien_0['color'])

alien_0 = {'color': 'green', 'points':5}
new_points = alien_0['points']
print("\nYou just earned " + str(new_points) + " points!\n")

#6.2.2 添加键-值对
alien_0['x_points'] = 0
alien_0['y_points'] = 25
print(alien_0)

# 6.2.3 先创建一个空字典
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
print(alien_0)

# 6.2.4 修改字典中的值
alien_0 = {'color': 'green'}
print("'\nThe alien is " + alien_0['color'] + ".")

alien_0['color'] = 'yellow'
print("The alien is " + alien_0['color'] + ".\n")


# 外星人
alien_0 = {'x_position' : 0, 'y_position' : 25, 'speed' : 'medium'}
print("Original x-position: " + str(alien_0['x_position']))

# 向右移动外星人
# 根据外星人当前速度决定将其移动距离
if alien_0['speed'] == 'slow':
    x_incremen = 1
# elif alien_0['speed'] == 'medium':
elif alien_0['speed'] == 'fast':
    x_incremen = 2
else:
    # 这个外星人的速度一定很快
    x_incremen = 3
# 新位置等于老位置加上增量
alien_0['x_position'] = alien_0['x_position'] + x_incremen
print("New x-position: " + str(alien_0['x_position']))

# 6.2.5 删除键-值对
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)

del alien_0['points']
print(alien_0)

# 6.5.6 由类似对象组成的字典
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}

print("Sarah's favorite language is " +
      favorite_languages['sarah'].title() +
      ".\n")
# favorite_languages['sarah']


# 6.4.1 字典列表
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}

aliens = [alien_0, alien_1, alien_2]

for alien in aliens:
    print(alien)

print("\n")

# range() 生成30个外星人
aliens = []
for alien_number in range(30):
    new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
    aliens.append(new_alien)

for alien in aliens[0: 3]:
    if alien['color'] == 'green':
        alien['color'] = 'yellow'
        alien['speed'] = 'slow'
        alien['points'] = 10
    elif alien['color'] == 'yellow':
        alien['color'] = 'red'
        alien['speed'] = 'gast'
        alien['points'] = 15

for alien in aliens[:5]:
    print(alien)
print("..")

print("Total number of aliens: " + str(len(aliens))+ "\n")

# 6.4.2 在字典中储存列表
# 储存所点的披萨的信息
pizaa = {
    'crust': 'thick',
    'toppings': ['mushroom', 'extra cheese'],
}

# 概述所点的披萨
print("Your ordeed a " + pizaa['crust'] + "-crust pizza "+ "with the following toppings: ")

for topping in pizaa['toppings']:
    print("\t" + topping)


favorite_languages = {
    'jen': ['python', 'ruby'],
    'sarah': ['c'],
    'edward': ['ruby', 'go'],
    'phil': ['python', 'haskell'],
}
for name, languages in favorite_languages.items():
    print("\n" + name.title() + "'s favorite languages are:")
    for language in languages:
        print("\t" + language.title())

# 6.4.3 在字典中储存字典 many_users.py
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'pricnceton',
    },

    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
    },
}
for username, user_info in users.items():
    print("\nUsername: "+ username)
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']

    print("\tFull name = " + full_name.title())
    print("\tLocation: " + location.title()+ "\n")


# 实战
# 6-7 人
alien_01 = {'color': 'green', 'points':5}
alien_02 = {'color': 'red', 'points':15}
alien_03 = {'color': 'yellow', 'points':25}
people = [alien_01, alien_02, alien_03]
for mg in people:
    print(mg)

# 6-8 宠物
print("\n")
pets = {
    'dog': {
        'name': '拆迁办主任',
        'age': '2',
        'type': '灰狼'
    },
    'cat': {
        'name': '暖暖',
        'age': '3',
        'type': '猫'
    },
}
for Animal_name, Animal_info in pets.items():
    print("Animal_name = " + Animal_name.title())
    animal_name = Animal_info['name']
    animal_age = Animal_info['age']
    animal_type = Animal_info['type']
    print("\tAnimal_name: " + animal_name)
    print("\tAnimal_age: " + animal_age)
    print("\tanimal_type: "+ animal_type, "\n")


# 6-9 喜欢的地方
favorite_places = {
    'jack': {
        'one': '家',
        'two': '公园',
        'three': '商场'

    },
    'tom': {
        'one': '游戏机厅',
        'two': '电影院',
        'three': 'ktv'
    },
    'mori': {
        'one': '网咖',
        'two': '篮球场',
        'three': '咖啡厅'
    },
}
for favorite_name, favorite_placess in favorite_places.items():
    print("\nfavorite_name: " + favorite_name.title())
    first_places = favorite_placess['one']
    seconde_places = favorite_placess['two']
    third_places = favorite_placess['three']
    print("\tfirst_places: " + first_places.title())
    print("\tseconde_places: " + seconde_places.title())
    print("\tthird_places: " + third_places.title())

# 6-10 喜欢的数字
print("\n")
numbers = {
    'captain': {
        'one':'1',
        'two':'2',
        'three': '3'
    },
    'ewa': {
        'one':'1',
        'two':'2',
        'three': '3'
    },
    'wali': {
        'one':'1',
        'two':'2',
        'three': '3'
    },
    'Sherlock Holmes': {
        'one':'1',
        'two':'2',
        'three': '3'
    },
    'John·H·Watson': {
        'one':'1',
        'two':'2',
        'three': '3'
    },
}
for names_num, like_num in numbers.items():
    print("\nNumbers_name: " + names_num.title())
    like_1 = like_num['one']
    like_2 = like_num['two']
    like_3 = like_num['three']
    print("\tLike number: " + like_1)
    print("\tLike number: " + like_2)
    print("\tLike number: " + like_3)

# 6-10 城市
print("\n")
cities = {
    'BeiJing': {
        'country': 'china',
        'population': '2171Thousand',
        'fact': 'capital',
    },
    'ShangHai': {
        'country': 'china',
        'population': '2415Thousand',
        'fact': 'Municipality',
    },
    'ShenZhen': {
        'country': 'china',
        'population': '1190.84Thousand',
        'fact': 'sub provincial city',
    },
}
for cities_name, cities_info in cities.items():
    print("\nCities name: " + cities_name.title())
    cities_country = cities_info['country']
    cities_population = cities_info['population']
    cities_fact = cities_info['fact']
    print("\tities Country: " + cities_country.title())
    print("\tCities population: " + cities_population.title())
    print("\tCities cities_fact: " + cities_fact.title())